<?php

/**
 * client actions.
 *
 * @package    student_manager
 * @subpackage client
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class clientActions extends autoclientActions
{


  public function executeList()
  {
     $this->getResponse()->setTitle('WMS-Client-List');
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/client/filters');

    // pager
    $this->pager = new sfPropelPager('Client', 25);
    $c = new Criteria();
    $c->addDescendingOrderByColumn(ClientPeer::ID);
    
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/client')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/client');
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['name_is_empty']))
    {
      $criterion = $c->getNewCriterion(ClientPeer::NAME, '');
      $criterion->addOr($c->getNewCriterion(ClientPeer::NAME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['name']) && $this->filters['name'] !== '')
    {
      $c->add(ClientPeer::NAME, '%'.strtr($this->filters['name'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['contactpersonname_is_empty']))
    {
      $criterion = $c->getNewCriterion(ClientPeer::CONTACTPERSONNAME, '');
      $criterion->addOr($c->getNewCriterion(ClientPeer::CONTACTPERSONNAME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['contactpersonname']) && $this->filters['contactpersonname'] !== '')
    {
      $c->add(ClientPeer::CONTACTPERSONNAME, '%'.strtr($this->filters['contactpersonname'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['created_at_is_empty']))
    {
      $criterion = $c->getNewCriterion(ClientPeer::CREATED_AT, '');
      $criterion->addOr($c->getNewCriterion(ClientPeer::CREATED_AT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_at']))
    {
      if (isset($this->filters['created_at']['from']) && $this->filters['created_at']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(ClientPeer::CREATED_AT, $this->filters['created_at']['from'], Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_at']['to']) && $this->filters['created_at']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(ClientPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(ClientPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
  }

}
